package br.com.Servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.Dao.EnderecoDao;
import br.com.Dao.PaginaDao;
import br.com.Dao.UsuarioComercianteDao;
import br.com.Modelo.Endereco;
import br.com.Modelo.Pagina;
import br.com.Modelo.UsuarioComerciante;

/**
 * Servlet implementation class editar_comerciante
 */
@WebServlet("/admin/editar_comerciantes")
public class editar_comerciante extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public editar_comerciante() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  System.out.print("entrou aqiu");
		  
		if(!response.isCommitted ()) 
		request.getRequestDispatcher("config_admin.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UsuarioComerciante comerciante = new UsuarioComerciante();
		comerciante.setLogin(request.getParameter("login_empresa"));	
		comerciante.setSenha(request.getParameter("senha_empresa"));
		comerciante.setNome(request.getParameter("nome_empresa"));
		comerciante.setCnpj(request.getParameter("cnpj"));
		comerciante.setRamo(request.getParameter("ramo_empresa"));
		comerciante.setId(Long.parseLong(request.getParameter("id_comerciante")));
	    //String perfil_pagina = request.getParameter("perfil_pagina"); 
		
		  System.out.print("entrou aqiu");
	  

   	UsuarioComercianteDao dao = new UsuarioComercianteDao();
   
   	

   	
    if(comerciante.getLogin()==""||
            comerciante.getSenha()=="" ||
            comerciante.getNome() == "" || 
            comerciante.getCnpj() == ""||         
            comerciante.getRamo()==""){
          	
    	request.getRequestDispatcher("config_admin.jsp?check-cad=Campos Invalido!!!").forward(request, response);
          //response.sendRedirect("/plataforma_marketing/admin/config_admin.jsp?check-cad=Campos Invalido!");
          
    }else if(dao.verificaUsuario(comerciante.getLogin(),comerciante.getCnpj())==1){
		
    	request.getRequestDispatcher("config_admin.jsp?check-cad=Login ou Cnpj já Cadastrado!!").forward(request, response);
    	 // response.sendRedirect("/plataforma_marketing/admin/config_admin.jsp?check-cad=Login ou Cnpj já Cadastrado!!");
    	//response.sendRedirect("cadastro.jsp?check-cad=Campos Invalidos!!");
    	
    	
    	
    }else{   	
   
   	    dao.updateComerciante(comerciante);
   	    response.sendRedirect("/plataforma_marketing/admin/config_admin.jsp?check-ok= Comerciante Atualizado!");
   	    
    }
    
      
    }

}
